package com.put.mybills.activities;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.put.mybills.R;
import com.put.mybills.adapter.GlobalParams;
import com.put.mybills.model.Facture;
import com.put.mybills.model.Unit;

import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class FactureAnticipationFragment extends Fragment {
	
	private String factureName;
	private Facture facture;
	private List<Unit> allUnit;
	
	private TextView unitResult;

	private EditText unitMetic;
	private EditText unitPrice;
	private EditText unitValue;
	
	private TextView summaryUnit;
	
	private TableLayout unitResultTable;
	private Spinner UnitList;
	
	private LinkedList<List<String>> unitStack = new LinkedList<List<String>>();
	
	
	private ImageButton addUnit;
	private ImageButton delUnit;
	
	public FactureAnticipationFragment(String factureName) {
		this.factureName = factureName;
	}
	
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		
		
		View rootView = inflater.inflate(R.layout.fragment_anticipation, container, false);
	    
		
		unitResultTable = (TableLayout)  rootView.findViewById(R.id.table_units_result);	
		unitMetic = (EditText) rootView.findViewById(R.id.unit_metric_value);
		unitPrice = (EditText) rootView.findViewById(R.id.unit_price_value);
		unitValue = (EditText) rootView.findViewById(R.id.unit_value_value);
		
		addUnit = (ImageButton) rootView.findViewById(R.id.add_units);
		delUnit = (ImageButton) rootView.findViewById(R.id.delete_units);
		UnitList = (Spinner) rootView.findViewById(R.id.unitlist);
		facture = GlobalParams.getInstance().facturedatasource.getFacture(factureName);
		
		summaryUnit = (TextView) rootView.findViewById(R.id.facture_anticipation_summary);
		
		
		addUnit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if (UnitList.getAdapter().getCount()>0){
					String name = UnitList.getSelectedItem().toString();
					String price = unitPrice.getText().toString();
					String metric = unitMetic.getText().toString();
					String value = unitValue.getText().toString();
					
					List<String> unitList = new ArrayList<String>();
					unitList.add(name);
					unitList.add(price);
					unitList.add(metric);
					unitList.add(value);
					
					if (price.length()>0 && value.length()>0)				
						unitStack.addLast(unitList);
					
					createTableUnitResult();
				}
			}
		});
		
		
		delUnit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (unitStack.size()>0)
					unitStack.removeLast();
				
				createTableUnitResult();
				
			}
		});
		
		
		
		allUnit = GlobalParams.getInstance().unitdatasource.getAllUnitForFacture(facture.getId());
		List<String> unitNames = new ArrayList<String>();
		
		for (int i=0;  i<allUnit.size() ; i++)
			unitNames.add(allUnit.get(i).getName());
		
		String[] valuesUnit =  unitNames.toArray(new String[unitNames.size()]);		
		
	
		ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, valuesUnit);
		adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		unitMetic.setText("");
		unitPrice.setText("");
		unitValue.setText("");
		
		
		UnitList.setAdapter(adapter1);
		UnitList.setOnItemSelectedListener(new OnItemSelectedListener() {			
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {								
						
						unitMetic.setText("");
						unitPrice.setText("");
						unitValue.setText("");
				
						if (allUnit.size()>0){
							Unit newUnit = allUnit.get(position);
							unitMetic.setText(newUnit.getMetric()); 
							unitPrice.setText(newUnit.getPrice().toString());
						}
	
					}
			@Override
			public void onNothingSelected(AdapterView<?> parent) {}			
		});
		

		return rootView;
	
	}
	
	
	 public void createTableUnitResult() {
	    	
	    	unitResultTable.removeAllViews();
	    	
	    	Double summary = 0.0;
	    	
		 	for (List<String> st : unitStack) {	
		 		
		 		TableRow row = new TableRow(getActivity());

			    TextView unitName = new TextView(getActivity());
			    TextView unitPrice = new TextView(getActivity());
			    TextView unitMetric = new TextView(getActivity());
			    TextView unitValue = new TextView(getActivity());
			    
			    unitName.setText(st.get(0));
			    unitName.setBackgroundResource(R.drawable.cell_shape);
			    
			    unitPrice.setText(st.get(1));
			    unitPrice.setBackgroundResource(R.drawable.cell_shape);
			    
			    unitMetric.setText(st.get(2));
			    unitMetric.setBackgroundResource(R.drawable.cell_shape);
			   
			    unitValue.setText(st.get(3));
			    unitValue.setBackgroundResource(R.drawable.cell_shape);
			   
			    row.addView(unitName);
			    row.addView(unitPrice);
			    row.addView(unitMetric);
			    row.addView(unitValue);
			    
			    unitResultTable.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			    
			    Double p = Double.parseDouble(st.get(1));
			    Double v = Double.parseDouble(st.get(3));
			    
			    summary += p*v;
			    
				
		 	}
		 	
		 	summaryUnit.setText(summary.toString());

	    }

}
